cd "C:\Users\Yueyuan Ma\Box\Specialization\Replication_Files"

use "Data\Patent_Data\assignment.dta", clear
duplicates drop
keep rf_id convey_text record_dt
gen record_year=year(record_dt)
save "Data\assign_id.dta",replace

use "Data\Patent_Data\assignor.dta", clear
duplicates drop

bys rf_id (exec_dt): egen exec_last=max(exec_dt)
format exec_last %td

keep rf_id exec_last
duplicates drop
save "Data\assignor_rv.dta",replace

use "Data\assign_id.dta",clear
merge 1:1 rf_id using "Data\Patent_Data\assignment_conveyance.dta"
drop _m

gen convey_ty_new=convey_ty
replace convey_ty_new="employee" if employer_assign==1

merge 1:1 rf_id using "Data\assignor_rv.dta"
drop _m
gen exec_year=year(exec_last)
save "Data\assign_inf.dta",replace

use "Data\Patent_Data\documentid.dta",clear
keep rf_id appno_doc_num appno_date grant_doc_num grant_date pgpub_doc_num pgpub_date
merge m:1 rf_id appno_doc_num grant_doc_num using "Data\Patent_Data\documentid_admin.dta"
drop _m
merge m:1 rf_id using "Data\assign_inf.dta"
drop _m
save "Data\property_inf.dta",replace

use "Data\property_inf.dta",clear
keep if error=="none"
duplicates drop rf_id appno_doc_num grant_doc_num convey_ty_new exec_last, force

gen app_year=year(appno_date)
gen grant_year=year(grant_date)

keep if convey_ty_new=="assignment" | convey_ty_new=="merger"
drop if appno_doc_num=="" |  grant_doc_num==""
drop if exec_last==.
rename admin_pat_no_for_appno patent

gen dummy=1
bys patent (exec_last): gen exec_seq=sum(dummy)
keep if exec_seq==1
drop dummy exec_seq

gen patent_first=substr(patent,1,1)
gen patent_second=substr(patent,2,1)
drop if patent_first=="D" | patent_first=="H" | patent_first=="P" | patent_first=="R"| patent_first=="T"
drop if patent_second=="E" | patent_second=="P"

destring patent,replace

save "Data\property_nodup.dta",replace

use "Data\Patent_Data\annual.dta",clear
keep year total_app total_inforce total_iss
save "Data\patent_count.dta",replace

use "Data\Patent_Data\pat76_06_assg.dta",clear
gen n=1
gen cite=allcite*hjtwt
collapse (sum) n cite, by(gyear)
rename n total_iss
rename cite total_cite
rename gyear year
save "Data\patent_count_pdp.dta",replace


/*By Reassignment Type*/

use "Data\property_nodup.dta",clear
gen diff=exec_year-grant_year
gen reass_0=1 if diff>=-4 & diff<=0
gen reass_1=1 if diff>=1 & diff<=5
gen reass_2=1 if diff>=6 & diff<=10
gen reass_3=1 if diff>=11 & diff~=.

replace reass_0=0 if reass_0==.
replace reass_1=0 if reass_1==.
replace reass_2=0 if reass_2==.
replace reass_3=0 if reass_3==.


gen n=1
collapse (sum) n reass_0 reass_1 reass_2 reass_3, by ( grant_year convey_ty_new )
rename n reass_count
gen year=grant_year
drop if year==.

merge n:1 year using "Data\patent_count_pdp.dta"
drop if _m==2
drop _m

gen reass_grt_ratio=reass_count/total_iss
gen reass_grt_ratio0=reass_0/total_iss
gen reass_grt_ratio1=reass_1/total_iss
gen reass_grt_ratio2=reass_2/total_iss
gen reass_grt_ratio3=reass_3/total_iss


save "Data\Reassign_Ratio_by_Assigntype.dta",replace

